In this part of the tutorial, we will see how to query RDF Streams under an simple entailment regime using a stream reasoner called Jasper.
The JASPER is an RSP engine that offers rule-based reasoning capabilities. It consumes queries written in RSP-QL syntax and fully implements the RSP-QL reference model.
RSPLib is the python library that we are going to use in our tutorial. It offers abstrations to manipulate RSPSources, e.g. RDF Streams and to interact with RSP Engines, e.g. registering streams and queries.
RSPLib APIs are still under development. In this tutorial we are going to use version 0.3.4 which is available at https://pypi.python.org/pypi/rsplib/.
to upgrade it just type !pip instal rsplib --upgrade in a cell
source code available at https://github.com/streamreasoning/rsplib
In [37]:
from IPython.display import Image
from IPython.core.display import HTML
from rsplib.processing import RSPSource, StreamReasoner
Now that we have our RDF Stream running, we can start thinking about how to query them. To this extent, we are going to use jasper.
Did you started csparql in the consumer folder?
RSPLib offers a facade to communicate with Stream Reasoners via the SR Services (a RESTful interface for SRs).
In [21]:
jasper = StreamReasoner("http://jasper", 8183);
jasper.status()
Out[21]:
In [18]:
jasper.register_stream("AarhusTrafficData158505", "http://aarhustrafficdata158505:4001/sgraph")
Out[18]:
In [19]:
jasper.register_stream("AarhusTrafficData182955", "http://aarhustrafficdata182955:4000/sgraph")
Out[19]:
In order to perform some reasoning, we require to capture some knowledge about our domain.
In this example the ontology available at here
contains a simple hierarchi
CongestionLevelA and CongenstionLevelB are subclasses of CongestionLevelT.
In [41]:
Image(url= "https://raw.githubusercontent.com/streamreasoning/rsplab/tutorial/collector/lab/streamapp/images/tbox.png")
Out[41]:
If we have a look to our streams again:
we will observer that either CongestionLevelA or CongenstionLevelB are present.
We are now going to show how to look for CongestionLevelT adding some reasoning.
In [40]:
tbox = "https://raw.githubusercontent.com/streamreasoning/rsplab/tutorial/collector/lab/streamapp/tbox.rdf"
In [15]:
with open('rdfs.rules.txt', 'r') as rule_file:
rdfs=rule_file.read()
rdfs
Out[15]:
In [16]:
jasper.register_rules("rdfs", rdfs)
Out[16]:
In [17]:
jasper.rules()
Out[17]:
the syntax we are now using is called RSP-QL syntax. The rsp w3c working group is currently working on its specification. Jasper is an early adopter for this syntax.
In [20]:
with open('q4.rspql.txt', 'r') as rspql_query:
body = rspql_query.read()
print(body)
In [25]:
jasper.register_query("ct", "STREAM", body, "rdfs", tbox)
Out[25]:
In [29]:
jasper.register_observer("ct", "default", {"host":"jasper","type":"ws","port":8283,"name":"default"})
Out[29]:
Are you courious how the output stream looks like?
http://localhost:8888/notebooks/work/streamapp/Observing%20The%20Output%20-%20Part%202.ipynb
RSPLab offers assisted realt-time performance monitoring using cAdvisor and Grafana. In order to observe the current status of the engine you can visit
http://localhost:3000/dashboard/db/jasper?orgId=1
in case you didn't access yet, username:admin password:admin
In [42]:
Image(url= "https://raw.githubusercontent.com/streamreasoning/rsplab/tutorial/collector/lab/streamapp/images/csparq_grafana.png")
Out[42]:
In [30]:
jasper.unregister_observer("ct", "default")
Out[30]:
In [31]:
jasper.unregister_query("ct")
Out[31]:
In [33]:
jasper.unregister_stream("AarhusTrafficData182955")
Out[33]:
In [34]:
jasper.unregister_stream("AarhusTrafficData158505")
Out[34]:
In [35]:
jasper.status()
Out[35]:
In [ ]: